package net.zjitc.controller;

import net.zjitc.pojo.CartItem;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.Product;
import net.zjitc.pojo.User;
import net.zjitc.service.CartService;
import net.zjitc.service.CategoryService;
import net.zjitc.service.impl.CartServiceImpl;
import net.zjitc.service.impl.CategoryServiceImpl;
import net.zjitc.utils.dispacher.URLMapping;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/11.
 */
public class UserCart {
    CartService cartService=new CartServiceImpl();
    @URLMapping(url = "/findMyCart")
    public String findMyCart(HttpServletRequest request, HttpServletResponse response){
        User user= (User) request.getSession().getAttribute("session_user");
        //如果未登录，则返回登录界面
        if (user==null){
            return "/WEB-INF/user/login.jsp";
        }
        //如果登录
        List<CartItem> list=cartService.findByUid(user.getUid());
        request.setAttribute("list",list);
        //如果购物车为空则直接转发
        if (list==null){
            return "/WEB-INF/user/cart.jsp";
        }
        //计算总价
        double total=0.0;
        for (CartItem c:list){
            total=total+c.getQuantity()*c.getProduct().getShop_price();
        }
        request.setAttribute("total",total);
        //菜单栏显示一级菜单
        CategoryService service=new CategoryServiceImpl();
        List<Category> clist=service.findAll();
        request.setAttribute("clist",clist);
        return "/WEB-INF/user/cart.jsp";
    }

    //添加购物车
    @URLMapping(url = "/addCart")
    public String shopIndex(HttpServletRequest request, HttpServletResponse response){
        //
        Map<String, String[]> map=request.getParameterMap();
        Product product=new Product();
        CartItem cartItem=new CartItem();
        User user= (User) request.getSession().getAttribute("session_user");
        if (user==null){
            return "Redirect:"+request.getContextPath()+"/loginUI";
        }
        try {
            BeanUtils.populate(product,map);
            BeanUtils.populate(cartItem,map);
            cartItem.setProduct(product);
            cartItem.setUser(user);
            int count=cartService.add(cartItem);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return "Redirect:"+request.getContextPath()+"/findMyCart";
    }
    //删除购物车条目
    @URLMapping(url = "/removeCart")
    public String removeCart(HttpServletRequest request, HttpServletResponse response){
        String cartid=request.getParameter("cartid");
        int count=cartService.deleteCart(cartid);
        return "Redirect:"+request.getContextPath()+"/findMyCart";
    }
    //清空购物车
    @URLMapping(url = "/clearCart")
    public String clearCart(HttpServletRequest request, HttpServletResponse response){
        User user= (User) request.getSession().getAttribute("session_user");
        int count=cartService.clearCart(user);
        return "Redirect:"+request.getContextPath()+"/findMyCart";
    }
}
